home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / hf^k-6.dms / in.adf / Install.run / GOLDEDDATA / developer / examples / api / startup / lib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  4.7 KB  |  178 lines

  1. /*
  2.  *  LIB.C
  3.  *
  4.  *  Basic Library Resource Handling
  5.  *
  6.  *  NOTE: all data declarations should be initialized since we skip
  7.  *        normal C startup code (unless initial value is don't care)
  8.  *
  9.  *  WARNING: arguments are passed in certain registers from the assembly
  10.  *        tag file, matched to how they are declared below.  Do not change
  11.  *        the argument declarations!
  12.  */
  13.  
  14. #include "defs.h"
  15.  
  16. Prototype LibCall struct Library *LibInit   (__D0 BPTR);
  17. Prototype LibCall struct Library *LibOpen   (__D0 long, __A0 struct Library *);
  18. Prototype LibCall long            LibClose  (__A0 struct Library *);
  19. Prototype LibCall long            LibExpunge(__A0 struct Library *);
  20.  
  21. struct Library  *LibBase       = NULL;
  22. struct ExecBase *SysBase       = NULL;
  23. struct Library  *DOSBase       = NULL;
  24. struct Library  *IntuitionBase = NULL;
  25. struct Library  *RexxSysBase   = NULL;
  26.  
  27. BPTR SegList = 0;
  28.  
  29. /*
  30.  *    The Initialization routine is given only a seglist pointer.  Since
  31.  *    we are NOT AUTOINIT we must construct and add the library ourselves
  32.  *    and return either NULL or the library pointer.  Exec has Forbid()
  33.  *    for us during the call.
  34.  *
  35.  *    We use an extended library structure to allow identification as an
  36.  *    API client
  37.  */
  38.  
  39. LibCall struct Library *
  40. LibInit(__D0 BPTR segment)
  41. {
  42.     struct Library *lib;
  43.  
  44.     static const long Vectors[] = {
  45.  
  46.         (long)ALibOpen,
  47.         (long)ALibClose,
  48.         (long)ALibExpunge,
  49.         (long)ALibReserved,
  50.         (long)APIMountClient,
  51.         (long)APICloseClient,
  52.         (long)APIBriefClient,
  53.         (long)APIFree,
  54.         -1
  55.     };
  56.  
  57.     SysBase = (struct ExecBase *)*(long *)4;
  58.  
  59.     if (DOSBase = OpenLibrary("dos.library", 0)) {
  60.  
  61.         if (IntuitionBase = OpenLibrary("intuition.library", 0)) {
  62.  
  63.             if (RexxSysBase = OpenLibrary("rexxsyslib.library", 36)) {
  64.  
  65.                 if (LibBase = lib = MakeLibrary((APTR)Vectors, NULL, NULL, sizeof(struct APIBase), NULL)) {
  66.  
  67.                     lib->lib_Node.ln_Type = NT_LIBRARY;
  68.                     lib->lib_Node.ln_Name = LibName;
  69.                     lib->lib_Flags        = LIBF_CHANGED | LIBF_SUMUSED;
  70.                     lib->lib_Version      = 37;
  71.                     lib->lib_Revision     = 0;
  72.                     lib->lib_IdString     = (APTR)LibId;
  73.  
  74.                     ((struct APIBase *)lib)->Magic = API_MAGIC;
  75.  
  76.                     SegList = segment;
  77.  
  78.                     AddLibrary(lib);
  79.  
  80.                     InitC();
  81.  
  82.                     return(lib);
  83.                 }
  84.             }
  85.         }
  86.     }
  87.  
  88.     return(NULL);
  89. }
  90.  
  91. /*
  92.  *    Open is given the library pointer and the version request.  Either
  93.  *    return the library pointer or NULL.  Remove the DELAYED-EXPUNGE flag.
  94.  *    Exec has Forbid() for us during the call.
  95.  */
  96.  
  97. LibCall struct Library *
  98. LibOpen(__D0 long version, __A0 struct Library *lib)
  99. {
  100.     ++lib->lib_OpenCnt;
  101.  
  102.     lib->lib_Flags &= ~LIBF_DELEXP;
  103.  
  104.     return(lib);
  105. }
  106.  
  107. /*
  108.  *    Close is given the library pointer and the version request.  Be sure
  109.  *    not to decrement the open count if already zero.  If the open count
  110.  *    is or becomes zero AND there is a LIBF_DELEXP, we expunge the library
  111.  *    and return the seglist.  Otherwise we return NULL.
  112.  *
  113.  *    Note that this routine never sets LIBF_DELEXP on its own.
  114.  *
  115.  *    Exec has Forbid() for us during the call.
  116.  */
  117.  
  118. LibCall long
  119. LibClose(__A0 struct Library *lib)
  120. {
  121.     if (lib->lib_OpenCnt && --lib->lib_OpenCnt)
  122.         return(NULL);
  123.  
  124.     if (lib->lib_Flags & LIBF_DELEXP)
  125.         return(LibExpunge(lib));
  126.  
  127.     return(NULL);
  128. }
  129.  
  130. /*
  131.  *    We expunge the library and return the Seglist ONLY if the open count
  132.  *    is zero.  If the open count is not zero we set the DELAYED-EXPUNGE
  133.  *    flag and return NULL.
  134.  *
  135.  *    Exec has Forbid() for us during the call.  NOTE ALSO that Expunge
  136.  *    might be called from the memory allocator and thus we CANNOT DO A
  137.  *    Wait() or otherwise take a long time to complete (straight from RKM).
  138.  *
  139.  *    Apparently RemLibrary(lib) calls our expunge routine and would
  140.  *    therefore freeze if we called it ourselves.  As far as I can tell
  141.  *    from RKM, LibExpunge(lib) must remove the library itself as shown
  142.  *    below.
  143.  */
  144.  
  145. LibCall long
  146. LibExpunge(__A0 struct Library *lib)
  147. {
  148.     if (lib->lib_OpenCnt) {
  149.  
  150.         lib->lib_Flags |= LIBF_DELEXP;
  151.         return(NULL);
  152.     }
  153.  
  154.     Remove(&lib->lib_Node);
  155.  
  156.     FreeMem((char *)lib - lib->lib_NegSize, lib->lib_NegSize + lib->lib_PosSize);
  157.  
  158.     if (RexxSysBase) {
  159.  
  160.         CloseLibrary((struct Library *)RexxSysBase);
  161.         RexxSysBase = NULL;
  162.     }
  163.  
  164.     if (IntuitionBase) {
  165.  
  166.         CloseLibrary((struct Library *)IntuitionBase);
  167.         IntuitionBase = NULL;
  168.     }
  169.  
  170.     if (DOSBase) {
  171.  
  172.         CloseLibrary((struct Library *)DOSBase);
  173.         DOSBase = NULL;
  174.     }
  175.  
  176.     return((long)SegList);
  177. }
  178.